package com.cloudcc.ccTool;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpPost {
    public  String sendPost(String path,String param) {
        OutputStreamWriter out = null;
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();
        String exception = "";
        String result = "";
        try {
            URL url = new URL(path);
            HttpURLConnection conn = null;
            // 打开和URL之间的连接
            conn = (HttpURLConnection) url.openConnection();

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");    // POST方法
            conn.setConnectTimeout(15 * 1000);// 设置连接超时时间为5秒
            conn.setReadTimeout(20 * 1000);// 设置读取超时时间为20秒

            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            conn.setRequestProperty("Content-Type", "application/json");

            conn.connect();

            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream());
            // 发送请求参数
            BufferedWriter bw = new BufferedWriter(out);
            bw.write(param);
            // flush输出流的缓冲
            bw.flush();
            // 定义BufferedReader输入流来读取URL的响应
            br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            String line;
            while ((line = br.readLine())!=null){
                sb.append(line);
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            exception = "发送 POST 请求出现异常！"+e;
            e.printStackTrace();
        }finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(br!=null){
                    br.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        if(sb.toString().isEmpty()){
            result = exception;
        }else{
            result = sb.toString();
        }
        return result;
    }

    public  String postWithToken(String path,String param,String token) {
        OutputStreamWriter out = null;
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();
        String exception = "";
        String result = "";
        try {

            URL url = new URL(path);

            HttpURLConnection conn = null;

            // 打开和URL之间的连接
            conn = (HttpURLConnection) url.openConnection();

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");    // POST方法
            conn.setConnectTimeout(15 * 1000);// 设置连接超时时间为5秒
            conn.setReadTimeout(20 * 1000);// 设置读取超时时间为20秒

            // 设置通用的请求属性
//        	conn.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accesstoken", token);
            conn.connect();

            // 获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream());
            // 发送请求参数
            BufferedWriter bw = new BufferedWriter(out);
            bw.write(param);
            // flush输出流的缓冲
            bw.flush();
            // 定义BufferedReader输入流来读取URL的响应
            br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            String line;
            while ((line = br.readLine())!=null){
                sb.append(line);
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            exception = "发送 POST 请求出现异常！"+e;
            e.printStackTrace();
        }finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(br!=null){
                    br.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        if(sb.toString().isEmpty()){
            result = exception;
        }else{
            result = sb.toString();
        }
        return result;
    }
}
